#ifndef __RPC_MESSAGE_H__
#define __RPC_MESSAGE_H__

#include "ret.h"

#include <sys/time.h>
#include <pthread.h>

typedef enum _message_priority_t
{
    MESSAGE_PRIORITY_0 = 0,
    MESSAGE_PRIORITY_1,
    MESSAGE_PRIORITY_2,
    MESSAGE_PRIORITY_3,
    MESSAGE_PRIORITY_4,
    MESSAGE_PRIORITY_5,
    MESSAGE_PRIORITY_DEFAULT,
    MESSAGE_PRIORITY_6,
    MESSAGE_PRIORITY_7,
    MESSAGE_PRIORITY_8,
    MESSAGE_PRIORITY_9,
    MESSAGE_PRIORITY_UNKNOWN
}message_priority_t;

#pragma pack(1)
typedef struct _message_t{
    struct timeval time_created;
    message_priority_t priority;
    struct _message_t* next;
    size_t data_bytes;
    char data[0];
}message_t;
#pragma pack()

message_t* message_create(const size_t data_bytes,
    const message_priority_t priority);
void message_destroy(message_t** message);

typedef struct {
    message_t* head;
    message_t* tail;
    pthread_mutex_t mutex;
    size_t max_length;    
    size_t current_length;
    struct timeval latest_update_time;
}message_queue_t;

message_queue_t* message_queue_create(size_t max_size);
void message_queue_destroy(message_queue_t** queue);

ret_t message_enqueue(message_queue_t* queue,
    message_t* message);
message_t* message_dequeue(message_queue_t* queue);


#endif // __RPC_MESSAGE_H__